package com.project.platform.service.impl;

import com.project.platform.entity.*;
import com.project.platform.mapper.*;
import com.project.platform.service.BuildingService;
import com.project.platform.vo.Statistic;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.project.platform.vo.PageVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ${table.name}
 */
@Service
public class BuildingServiceImpl  implements BuildingService {
    @Resource
    private BuildingMapper buildingMapper;

    @Resource
    private RoomMapper roomMapper;

    @Resource
    private ElderMapper elderMapper;

    @Resource
    private CheckinApplicationMapper checkinApplicationMapper;

    @Resource
    private CheckoutApplicationMapper checkoutApplicationMapper;
    
    @Override
    public PageVO<Building> page(Map<String, Object> query, Integer pageNum, Integer pageSize) {
        PageVO<Building> page = new PageVO();
        List<Building> list = buildingMapper.queryPage((pageNum - 1) * pageSize, pageSize, query);
        page.setList(list);
        page.setTotal(buildingMapper.queryCount(query));
        return page;
    }

    @Override
    public Building selectById(Integer id) {
        Building building = buildingMapper.selectById(id);
        return building;
    }

    @Override
    public List<Building> list() {
        return buildingMapper.list();
    }
    @Override
    public void insert(Building entity) {
        check(entity);
        buildingMapper.insert(entity);
    }
    @Override
    public void updateById(Building entity) {
        check(entity);
        buildingMapper.updateById(entity);
    }
    private void check(Building entity) {

    }
    @Override
    public void removeByIds(List<Integer> ids) {
        buildingMapper.removeByIds(ids);
    }

    public List<Statistic> statistics() {
        List<Building> buildings = buildingMapper.list();
        List<Room> allRooms = roomMapper.list();
        List<Elder> allElders = elderMapper.list();
        List<CheckinApplication> checkinApps = checkinApplicationMapper.list();
        List<CheckoutApplication> checkoutApps = checkoutApplicationMapper.list();

        // 按楼栋分组统计房间容量
        Map<Integer, Integer> buildingCapacity = allRooms.stream()
                .collect(Collectors.groupingBy(Room::getBuildingId,
                        Collectors.summingInt(Room::getCapacity)));

        // 按楼栋分组统计已入住人数
        Map<Integer, Long> roomElderCount = allElders.stream()
                .collect(Collectors.groupingBy(elder -> {
                    Room room = allRooms.stream()
                            .filter(r -> r.getId().equals(elder.getRoomId()))
                            .findFirst()
                            .orElse(null);
                    return room != null ? room.getBuildingId() : 0;
                }, Collectors.counting()));

        // 按楼栋分组统计入住申请数
        Map<Integer, Long> checkinCounts = checkinApps.stream()
                .collect(Collectors.groupingBy(app -> {
                    Elder elder = allElders.stream()
                            .filter(e -> e.getId().equals(app.getElderId()))
                            .findFirst()
                            .orElse(null);
                    if (elder == null) return null;
                    Room room = allRooms.stream()
                            .filter(r -> r.getId().equals(elder.getRoomId()))
                            .findFirst()
                            .orElse(null);
                    return room != null ? room.getBuildingId() : 0;
                }, Collectors.counting()));

        // 按楼栋分组统计退住申请数
        Map<Integer, Long> checkoutCounts = checkoutApps.stream()
                .collect(Collectors.groupingBy(app -> {
                    Elder elder = allElders.stream()
                            .filter(e -> e.getId().equals(app.getElderId()))
                            .findFirst()
                            .orElse(null);
                    if (elder == null) return 0;
                    Room room = allRooms.stream()
                            .filter(r -> r.getId().equals(elder.getRoomId()))
                            .findFirst()
                            .orElse(null);
                    return room != null ? room.getBuildingId() : 0;
                }, Collectors.counting()));

        return buildings.stream().map(building -> {
            Statistic stat = new Statistic();
            stat.setId(building.getId());
            stat.setName(building.getName());

            // 设置统计指标
            stat.setTotal(buildingCapacity.getOrDefault(building.getId(), 0));
            stat.setCurrent(roomElderCount.getOrDefault(building.getId(), 0L).intValue());
            stat.setCheckinApps(checkinCounts.getOrDefault(building.getId(), 0L).intValue());
            stat.setCheckoutApps(checkoutCounts.getOrDefault(building.getId(), 0L).intValue());

            return stat;
        }).collect(Collectors.toList());
    }
}
